package com.neusoft.elmboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.elmboot.Dto.CartDto;
import com.neusoft.elmboot.Vo.BussinessVo;
import com.neusoft.elmboot.Vo.CartVo;
import com.neusoft.elmboot.Vo.FoodVo;
import com.neusoft.elmboot.common.result.Result;
import com.neusoft.elmboot.po.BusinessEntity;
import com.neusoft.elmboot.po.CartEntity;
import com.neusoft.elmboot.po.FoodEntity;
import com.neusoft.elmboot.service.BusinessService;
import com.neusoft.elmboot.service.CartService;
import com.neusoft.elmboot.service.FoodService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/carts")
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private FoodService foodService;

    @Autowired
    private BusinessService businessService;

    @GetMapping("/listCart")
    public Result<List<CartVo>> getShoppingCart(String userId,Integer businessId) {
        LambdaQueryWrapper<CartEntity> lqw = new LambdaQueryWrapper<>();
        /*String userId = cartDto.getUserId();
        Integer businessId = cartDto.getBusinessId();*/
        lqw.eq(userId!=null, CartEntity::getUserId, userId);
        lqw.eq(businessId != null, CartEntity::getBusinessId, businessId);

        List<CartEntity> cartEntityList = cartService.list(lqw);

        

        /*BusinessEntity businessEntity = businessService.getById(businessId);
        BussinessVo  bussinessVo = new BussinessVo();
        BeanUtils.copyProperties(businessEntity,bussinessVo);*/
        



        List<CartVo> cartVoList = cartEntityList.stream().map((item) -> {
            
            Integer foodId = item.getFoodId();
            FoodEntity foodEntity = foodService.getById(foodId);
            FoodVo foodVo = new FoodVo();
            BeanUtils.copyProperties(foodEntity,foodVo);

            BusinessEntity business= businessService.getById(item.getBusinessId());
            BussinessVo bVo = new BussinessVo();
            BeanUtils.copyProperties(business,bVo);


            CartVo cartVo = new CartVo();
            BeanUtils.copyProperties(item, cartVo);
            cartVo.setFoodVo(foodVo);
            cartVo.setBussinessVo(bVo);
            

            return cartVo;
        }).collect(Collectors.toList());



        return Result.success(cartVoList);
    }

    /**
     * 加入购物车
     * @return
     */
    @ApiOperation("加入购物车")
    @PostMapping
    public Result saveCart(@RequestBody CartDto cartDto){
        log.info("加入购物车");
        if (cartDto.getQuantity()==null){
            cartDto.setQuantity(1);
        }
        CartEntity cartEntity = new CartEntity();
        BeanUtils.copyProperties(cartDto,cartEntity);
        cartService.getBaseMapper().insert(cartEntity);

        //cartService.save(cartEntity);

        return Result.success() ;

    }


    @PutMapping
    public Result editCart(@RequestBody CartDto cartDto) {
        CartEntity cartEntity = new CartEntity();
        BeanUtils.copyProperties(cartDto,cartEntity);
        cartService.removeById(cartEntity.getCartId());
        cartService.save(cartEntity);
        return Result.success();

    }

    @PostMapping("/remove")
    public Result remove(@RequestBody CartDto cartDto){
        CartEntity cartEntity = new CartEntity();
        BeanUtils.copyProperties(cartDto,cartEntity);
        cartService.removeById(cartEntity.getCartId());
        return Result.success();
    }

}
